<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button @click='layout' type="info">退出</el-button>
    </el-header>
    <!-- 页面主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="iscollapse?'64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单 -->
        <el-menu background-color="#313743" text-color="#fff" active-text-color="#359BFF" unique-opened
          :collapse="iscollapse" :collapse-transition="false" router :default-active="activepath">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单的模板 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+subItem.path+''" v-for="subItem in item.children" :key="subItem.id"
              @click="SaveNavState('/'+subItem.path)">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
  export default {
    data() {
      return {
        menuList: [],
        iconsObj: {
          '125': 'iconfont icon-users',
          '103': 'iconfont icon-tijikongjian',
          '101': 'iconfont icon-shangpin',
          '102': 'iconfont icon-danju',
          '145': 'iconfont icon-baobiao',
        },
        // 不折叠
        iscollapse: false,
        activepath: ''
      }
    },
    // created()  在模板渲染成html或者模板编译路由前调用
    created() {
      this.getMenuList()
      this.activepath = window.sessionStorage.getItem('activePath');
    },
    methods: {
      layout() {
        // 清空token
        window.sessionStorage.clear();
        // 编程式导航跳转到login页面
        this.$router.push('/login');
      },
      // 获取所有的菜单
      async getMenuList() {
        const {
          data: res
        } = await this.$http.get('menus');
        // console.log(res);
        if (res.meta.status !== 200) return this.message.error(res.meta.msg)
        this.menuList = res.data
      },
      //  点击按钮，切换菜单的折叠与展开
      toggleCollapse() {
        this.iscollapse = !this.iscollapse
      },
      SaveNavState(activepath) {
        window.sessionStorage.setItem('activePath', activepath)
        this.activepath = activepath
      }
    }
  }

</script>

<style lang="less" scoped>
  .home-container {
    height: 100%;
  }

  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 0;
    background-color: rgb(54, 61, 64);
    color: #fff;
    font-size: 20px;

    div {
      display: flex;
      align-items: center;

      span {
        margin-left: 15px;
      }
    }
  }

  .el-aside {
    background-color: rgb(49, 55, 67);

    .el-menu {
      border-right: none;
    }
  }

  .el-main {
    background-color: rgb(233, 237, 241);
  }

  .iconfont {
    margin-right: 10px;
  }

  .toggle-button {
    background-color: #4a5064;
    font-size: 10px;
    color: #fff;
    line-height: 24px;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }

</style>
